// pages/operat/fleedTools/packBox/packBox.js
const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    caseCode: "",
   // SingleCode: "",
    SingleCode:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  caseCode: function (e) {
    this.setData({
      caseCode: e.detail.value
    })
  },
  // SingleCode: function (e) {
  //   this.setData({
  //     SingleCode: e.detail.value
  //   })
  // },
  confirm: function () {  
    LApp._post({
      url: "nbapi/fleedtool/fillThebox",
      data: { caseCode: this.data.caseCode, SingleCode: this.data.SingleCode, userid: LApp.globalData.userInfo.UserID ,type:3},
      success: function (r) {
        wx.showToast({
          title: r.text,
          icon: 'success',
          duration: 2000
        });
      },
      success:function(){
        wx.showModal({
          title: '',
          content: '装箱成功',
        })

      },
      fail: function (r) {
        wx.showModal({
          title: '',
          content: '装箱失败',
        })

      }
    });
  },
  //扫描大箱
  caseCodesaoma: function () {
    var that = this;
    wx.scanCode({
      success: function (r) {
        if (r)
        {
          var codes = that.data.SingleCode;
          if (codes.indexOf(r.result) > -1) {
            wx.showToast({
              title: "单标和箱标重复",
            })
            return
          }

          LApp._get({
            url: 'nbapi/fleedtool/fillThebox/check',
            data: { code: r.result, userId: LApp.globalData.userInfo.UserID },
            success: function (res) {
              if (res) {
                that.setData({
                  caseCode: r.result,
                })
              }
            },
            fail: function (ex) {
              wx.showModal({
                title: '',
                content: ex,
              })
            }
          })
        }
      }
    });
  },
  SingleCodesaoma: function () {
    var that = this;
      wx.scanCode({
        success: function (r) {
          if (that.data.caseCode.indexOf(r.result) > -1){
            wx.showToast({
              title: "单标和箱标重复",
            })
            return
          }
          var codes = that.data.SingleCode;
          if (codes.indexOf(r.result) > -1) {
            wx.showToast({
              title: "该码已重复扫描",
            })
            return
          }
          LApp._get({
            url: 'nbapi/fleedtool/fillThebox/check',
            data: { code: r.result, userId: LApp.globalData.userInfo.UserID },
            success: function (res) {
             
              if (res) {
                codes.push(r.result);
                that.setData({
                  SingleCode: codes
                   })
                // if (that.data.SingleCode == null) {
                //   that.setData({
                //     SingleCode: [r.result]
                //   })
                // } else {
                //   that.setData({
                //     SingleCode: that.data.SingleCode + "," + r.result
                //   })
                // };  
              }
            },
            fail: function (ex) {
              wx.showModal({
                title: '',
                content: ex,
              })
            }
          })
                
        }
      });
  },
  ScanCheck:function(code)
  {

    var that=this;
    LApp._get({
      url: 'nbapi/fleedtool/fillThebox/check',
      data: { code: code, userId: LApp.globalData.userInfo.UserID},
      success: function (r) {
       if(r){
         that.setData({
           result: r
         })
       }
      },
      fail:function(ex){
        wx.showModal({
          title: '提示',
          content: ex,
        })
      }
    })

  }
})